<?php

namespace App\Imports;

use App\Models\Waybill;
use App\Models\Goods;
use App\Models\Setting;
use Maatwebsite\Excel\Concerns\ToModel;

class WaybillImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        // if ($row[0] == '淘宝订单' || !$row[0] || trim($row['12'])=='订单取消成功' || trim($row['13'])=='订单取消')
        if ($row[0] == '淘宝订单' || !$row[0] || trim($row['12'])=='订单取消成功' || trim($row['13'])=='订单取消')
        {
            return;
        }
        // dd($row);
        // Log::info($row[17]);

        // 清单申报状态: 0、待申报 1、已申报
        $declare_status = 1;
        if ($row[11]=='待申报')
        {
            $declare_status = 0;
        }

        // 平台发货状态 0、未发货, 1、已发货, 2、订单取消, 3、下单中,等待回执
        // $platform_delivery_arr = ['0' => '订单取消', '1' => '淘分销已发货!'];
        if (strpos($row[13], '取消')===false)
        {
            $platform_delivery_status = 1;
        }
        else
        {
            $platform_delivery_status = 0;
        }

        // 仓库下单状态 0、订单取消成功, 1、下单中,等待回执
        // $warehouse_delivery_arr = ['0'=>'订单取消成功', '1'=>'下单中,等待回执'];
        // if (in_array($row[12], $warehouse_delivery_arr))
        if (strpos($row[12], '取消')===false)
        {
            $warehouse_delivery_status = 1;
        }
        else
        {
            $warehouse_delivery_status = 0;
        }

        // 获取商品价格
        $goods_price = $cost_amount = $profit_amount = '';
        $status = 0;
        $goods = Goods::where('goods_bn', $row[6])->first();
        if ($goods)
        {
            if ($goods->sale_price)
            {
                $goods_price = sprintf('%.2f', $goods->sale_price * $row[7]);
            }

            if ($goods->cost_price)
            {
                $rate_euro = Setting::get('rate_euro');
                $cost_amount = sprintf('%.2f', $goods->euro_cost_price * $rate_euro['value'] * $row[7]);
            }

            if ($cost_amount)
            {
                $profit_amount =  sprintf('%.2f', ($goods->sale_price * $row[7]) - ($goods->cost_price * $row[7]));

                $status = 1;
            }
        }

        $waybill = Waybill::where('order_no', $row[0])->where('express_no', $row[1])->where('goods_bn', $row[6])->first();

        if (!$waybill)
        {
            $params = [
                'order_no' => $row[0],
                'express_no' => $row[1],
                'goods_name' => $row[2],
                'goods_bn' => $row[6],
                'part_bn' => '',
                'goods_num' => $row[7],
                'consignee_name' => $row[4],
                'consignee_address' => $row[5],
                'supplier_name' => $row[8],
                'seller_name' => $row[9],
                'order_amount' => $row[10],
                'status' => $status,
                'declare_status' => $declare_status,
                'platform_delivery_status' => $platform_delivery_status,
                'warehouse_delivery_status' => $warehouse_delivery_status,
                'clearance_type' => $row[15],
            ];
            if ($goods_price)
            {
                $params['goods_price'] = $goods_price;
            }
            if ($cost_amount)
            {
                $params['cost_amount'] = $cost_amount;
            }
            if ($profit_amount)
            {
                $params['profit_amount'] = $profit_amount;
            }
            return new Waybill($params);
        }
        else
        {
            $params = [
                'order_no' => $row[0],
                'express_no' => $row[1],
                'goods_name' => $row[2],
                'goods_bn' => $row[6],
                'part_bn' => '',
                'goods_num' => $row[7],
                'consignee_name' => $row[4],
                'consignee_address' => $row[5],
                'supplier_name' => $row[8],
                'seller_name' => $row[9],
                'order_amount' => $row[10],
                'status' => $status,
                'declare_status' => $declare_status,
                'platform_delivery_status' => $platform_delivery_status,
                'warehouse_delivery_status' => $warehouse_delivery_status,
                'clearance_type' => $row[15],
                'updated_at' => date('Y-m-d H:i:s')
            ];
            if ($goods_price)
            {
                $params['goods_price'] = $goods_price;
            }
            if ($cost_amount)
            {
                $params['cost_amount'] = $cost_amount;
            }
            if ($profit_amount)
            {
                $params['profit_amount'] = $profit_amount;
            }
            $waybill->update($params);
        }
    }

    //批量导入1000条
    public function batchSize(): int
    {
        return 100;
    }

    //以1000条数据基准切割数据
    public function chunkSize(): int
    {
        return 100;
    }
}
